/**
 * Copyright 2007 Jason Horman, Pete Aykroyd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.functor;

import java.util.Collection;
import java.util.Arrays;

/**
 * An abstract base class for org.functor prediates (functors that return a boolean value). This
 * base class allows the definition of base line boolean constructs (ANDs, ORs, and NOTs) and
 * methods to apply boolean rules to predicates that will apply to any predicate based on this
 * class.
 */
public abstract class BooleanFunctor<T extends BooleanFunctor> extends Functor {
	public abstract T and(T predicate);

	public abstract T or(T predicate);

	public abstract T not();

	public static <P extends BooleanFunctor> P or(Collection<P> predicates) {
		P expression = null;
		for (P predicate : predicates)
			if (expression == null)
				expression = predicate;
			else
				expression = (P) expression.or(predicate);
		return expression;
	}

	public static <P extends BooleanFunctor> P or(P... predicates) {
    return or(Arrays.asList(predicates));
	}

	public static <P extends BooleanFunctor> P and(Collection<P> predicates) {
		P expression = null;
		for (P predicate : predicates)
			if (expression == null)
				expression = predicate;
			else
				expression = (P) expression.and(predicate);
		return expression;
	}

	public static <P extends BooleanFunctor> P and(P... predicates) {
		return and(Arrays.asList(predicates));
	}
}